package com.mcodesal.framework.utils;

public class Vector2D {

    public double x, y;

    public Vector2D(double X, double Y) {
            x = X;
            y = Y;
    }

    public void setLocation(double x, double y) {
            this.x = x;
            this.y = y;
    }

    public boolean equals(Object o) {
            if (o instanceof Vector2D) {
                    Vector2D p = (Vector2D) o;
                    return p.x == x && p.y == y;
            }
            return false;
    }

    public void setX(double x) {
            this.x = x;
    }

    public void setY(double y) {
            this.y = y;
    }

    public double getX() {
            return x;
    }

    public double getY() {
            return y;
    }

    public int intX() {
            return (int) x;
    }

    public int intY() {
            return (int) y;
    }

    public void set(double x, double y) {
            this.x = x;
            this.y = y;
    }

    public Vector2D reverse() {
            x = -x;
            y = -y;
            return this;
    }

    public float length() {
            return (float) Math.sqrt(x * x + y * y);
    }

    public double lengthSquared() {
            return (x * x) + (y * y);
    }

    public Vector2D add(Vector2D vec2) {
            double x = this.x + vec2.x;
            double y = this.y + vec2.y;
            return new Vector2D(x, y);
    }

    public Vector2D subtract(Vector2D from) {
            double x = this.x - from.x;
            double y = this.y - from.y;
            return new Vector2D(x, y);
    }

    public double dot(Vector2D vec) {
            return (x * vec.x) + (y * vec.y);
    }

    public static double cross(Vector2D a, Vector2D b) {
            return a.cross(b);
    }

    public double cross(Vector2D vec) {
            return x * vec.y - y * vec.x;
    }

    public Vector2D scale(double a) {
            x *= a;
            y *= a;
            return this;
    }

    public Vector2D normalize() {
            double magnitude = Math.sqrt(dot(this));
            return new Vector2D(x / magnitude, y / magnitude);
    }
}
